package com.load.parsers;

import java.util.List;

import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.*;
import japa.parser.ast.visitor.VoidVisitorAdapter;

import com.database.objects.*;
import com.database.objects.Class;

public class ClassParser {

	public static void addClasses(CompilationUnit compilation, FileOO file) {
		ClassVisitor classVisitor = new ClassVisitor();
		classVisitor.visit(compilation, file);
	}

	private static class ClassVisitor extends VoidVisitorAdapter<FileOO> {
		@Override
		public void visit(ClassOrInterfaceDeclaration classFile, FileOO file) {
			super.visit(classFile, file);
			Class clazz = new Class(classFile.getName(), file.getStatus(), file);
			file.addChild(clazz);

			FieldVisitor fieldVisitor = new FieldVisitor();
			fieldVisitor.visit(classFile, clazz);
		}
	}

	private static class FieldVisitor extends VoidVisitorAdapter<Class> {
		@Override
		public void visit(FieldDeclaration fieldFile, Class clazz) {
			super.visit(fieldFile, clazz);
			List<VariableDeclarator> variables = fieldFile.getVariables();
			for (VariableDeclarator variable : variables) {
				String name = variable.getId().toString();
				clazz.addField(new Field(name));
			}
		}

	}

}